package com.wanwen.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanwen.entity.Asker;
import com.wanwen.entity.Role;
import com.wanwen.entity.RoleUsers;
import com.wanwen.entity.Users;
import com.wanwen.mapper.AskerMapper;
import com.wanwen.mapper.RoleMapper;
import com.wanwen.mapper.RoleUserMapper;
import com.wanwen.mapper.UserMapper;
import com.wanwen.utils.Result;
import com.wanwen.utils.ResultData;

@RestController
@RequestMapping("/roleUser")
public class RoleUserController {
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private UserMapper UserMapper;
	@Autowired
	private RoleUserMapper roleUserMapper;
	@Autowired
	private AskerMapper askerMapper;
	
	/**查询所有角色
	 * @param userId 用户Id
     * @return
     */
    @RequestMapping("/getRoleByUser")
    public Object getRoleByUser(String userId) {
    	//构建查询用户角色的wrapper并添加用户Id条件
    	QueryWrapper<RoleUsers> roleUserWrapper = new QueryWrapper<RoleUsers>().eq("users_id", userId);
        //根据用户Id查询用户角色表
        List<RoleUsers> userRoles = roleUserMapper.selectList(roleUserWrapper);
        List<Integer> roleIds=new ArrayList<Integer>();//初始化集合用来存储用户角色表查询出的角色Id
        for (RoleUsers item : userRoles) {//存储角色Id
        	roleIds.add(item.getRoleId());
		}
        //根据多个角色Id查询角色
        if(roleIds.size()!=0) {
        	List<Role> roles = roleMapper.selectList(new QueryWrapper<Role>().in("id", roleIds));
        	return new ResultData(0, "", roles,null);
        }else {
        	List<Role> roles = roleMapper.selectList(new QueryWrapper<Role>().in("id", 0));
        	return new ResultData(0, "", roles,null);
        }
        
    }
    
    /**查询所有咨询师
	 * @param userId 用户Id
     * @return
     */
    @RequestMapping("/getUserByRole")
    public Object getUserByRole() {
    	//构建查询用户角色的wrapper并添加用户Id条件
    	QueryWrapper<RoleUsers> roleUserWrapper = new QueryWrapper<RoleUsers>()
    			.eq("role_id", 2);
        //根据用户Id查询用户角色表
        List<RoleUsers> userRoles = roleUserMapper.selectList(roleUserWrapper);
        if(userRoles.size()!=0) {
        List<Integer> usersId=new ArrayList<Integer>();//初始化集合用来存储用户角色表查询出的用户Id
        for (RoleUsers item : userRoles) {//存储用户Id
        	usersId.add(item.getUsersId());
		}
        	List<Users> roles = UserMapper.selectList(new QueryWrapper<Users>().in("id", usersId));
        	return roles;
        }else {
        	return null;
        }
    }
    
    /**查询所有咨询师
   	 * @param userId 用户Id
        * @return
        */
       @RequestMapping("/getUserzxByRole")
       public Object getUserzxByRole() {
       	//构建查询用户角色的wrapper并添加用户Id条件
       	QueryWrapper<RoleUsers> roleUserWrapper = new QueryWrapper<RoleUsers>()
       			.eq("role_id", 4);
           //根据用户Id查询用户角色表
           List<RoleUsers> userRoles = roleUserMapper.selectList(roleUserWrapper);
           if(userRoles.size()!=0) {
           List<Integer> usersId=new ArrayList<Integer>();//初始化集合用来存储用户角色表查询出的用户Id
           for (RoleUsers item : userRoles) {//存储用户Id
           	usersId.add(item.getUsersId());
   		}
           	List<Users> roles = UserMapper.selectList(new QueryWrapper<Users>().in("id", usersId));
           	return roles;
           }else {
           	return null;
           }
       }
    
    /**根据用户Id分配角色
	 * @param userId 用户Id
     * @return
     */
    @RequestMapping("/addUserToRole")
    public Object addUserToRole(Integer usersId,String roleIds) {
    	String[] roleIdStr = roleIds.split(",");
    	boolean r = false;
    	for (String item : roleIdStr) {//存储角色Id
        	Integer roleid = Integer.parseInt(item);
        	RoleUsers RoleUsers = new RoleUsers(null, roleid, usersId);
        	if(roleUserMapper.insert(RoleUsers)==1) {
        		r=true;
        	}
        	if(item.equals("2")) {
        		QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>()
        				.eq("id", usersId);
				Users users = UserMapper.selectOne(queryWrapper);
        		askerMapper.insert(new Asker(null,usersId,users.getUname(),null,null));
        	}
		}
		return new Result(r==true?"成功":"失败", "", r);
    }
    
    /**根据用户Id移除角色
	 * @param userId 用户Id
     * @return
     */
    @RequestMapping("/delUserToRole")
    public Object delUserToRole(Integer usersId,String roleIds) {
    	String[] roleIdStr = roleIds.split(",");
    	List<Integer> rolelist=new ArrayList<Integer>();
    	for (String item : roleIdStr) {//存储角色Id
        	Integer roleid = Integer.parseInt(item);
        	rolelist.add(roleid);
		}
    	QueryWrapper<RoleUsers> roleUserWrapper = new QueryWrapper<RoleUsers>()
    			.eq("users_id", usersId)
    			.in("role_id", rolelist);
    	int delete = roleUserMapper.delete(roleUserWrapper);
    	QueryWrapper<Asker> queryWrapper = new QueryWrapper<Asker>();
    		queryWrapper.eq("uid", usersId);
		askerMapper.delete(queryWrapper);
		return new Result(delete!=0?"成功":"失败", "", delete!=0?true:false);
    }
}
